<?php
namespace App\Model;
use PhalApi\Model\NotORMModel as NotORM;

class Dynamic extends NotORM {
    
	/* 发布动态 */
	public function setDynamic($data) {
		
		$rs=\PhalApi\DI()->notorm->dynamics->insert($data);

		return $rs;
	}
	
	/* 评论/回复 */
    public function setComment($data) {
    	$dynamicid=$data['dynamicid'];
		/* 更新 动态 */
		\PhalApi\DI()->notorm->dynamics
            ->where("id = '{$dynamicid}'")
		 	->update( array('comments' => new \NotORM_Literal("comments + 1") ) );
	
        $res=\PhalApi\DI()->notorm->dynamics_comments
            ->insert($data);
			
		$videoinfo=\PhalApi\DI()->notorm->dynamics
					->select("comments")
					->where('id=?',$dynamicid)
					->fetchOne();
		$count=\PhalApi\DI()->notorm->dynamics_comments
					->where("commentid='{$data['commentid']}'")
					->count();
		$rs=array(
			'comments'=>$videoinfo['comments'],
			'replys'=>$count,
		);

		return $rs;	
    }
	
	/* 我的动态 */
    public function myVideo($uid,$p) {
        
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;
		
		$list=\PhalApi\DI()->notorm->dynamics
                    ->select('id,uid,title,thumb,href,likes,views,shares,status,isprivate,coin,reason,width,height')
                    ->where('uid=?',$uid)
                    ->order('id desc')
                    ->limit($start,$pnum)
                    ->fetchAll();

		return $list;
	}
    
    /* 动态列表 */
	public function getDynamiclists($where,$p,$order='addtime desc') {
        
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;

		$list=\PhalApi\DI()->notorm->dynamics
                    ->select('*')
                    ->where('status=1')
                    ->where($where)
                    ->order($order)
                    ->limit($start,$pnum)
                    ->fetchAll();

		return $list;
	}

 

	/* 更新 */
	/* public function upVideo($uid,$videoid,$data) {
		$rs=\PhalApi\DI()->notorm->dynamics->where('id=? and uid=?',$videoid,$uid)->update($data);
		return $rs;
	}
 */
	/* 获取动态信息 */
	public function getDynamic($dynamicid,$where='status=1') {
		$info=\PhalApi\DI()->notorm->dynamics
                ->select('id,uid,title,thumb,video_thumb,href,voice,length,likes,comments,type,width,height')
				->where('id = ?',$dynamicid)
                ->where($where)
				->fetchOne();
		return $info;
	}
    
   
    /* 是否点赞 */
	public function islike($uid,$dynamicid) {
        
		$isexist=\PhalApi\DI()->notorm->dynamics_like
						->select("id")
						->where("uid='{$uid}' and dynamicid='{$dynamicid}'")
						->fetchOne();
        if($isexist){
            return '1';
        }
        
		return '0';
	}
    
    /* 点赞+ */
	public function addLike($uid,$dynamicid) {
		$rs=\PhalApi\DI()->notorm->dynamics
				->where("id = ?",$dynamicid)
				->update( array('likes' => new \NotORM_Literal("likes + 1") ) );
                
        \PhalApi\DI()->notorm->dynamics_like
                    ->insert(array("uid"=>$uid,"dynamicid"=>$dynamicid,"addtime"=>time() ));
		return $rs;
	}
    
    /* 点赞- */
	public function reduceLike($uid,$dynamicid) {
		$rs=\PhalApi\DI()->notorm->dynamics
                    ->where("id = '{$dynamicid}' and likes>0")
                    ->update( array('likes' => new \NotORM_Literal("likes - 1") ) );
        
        \PhalApi\DI()->notorm->dynamics_like
                    ->where("uid='{$uid}' and dynamicid='{$dynamicid}'")
                    ->delete();
                            
		return $rs;
	}
    public function getDynamicComment($commentid){
		//根据commentid获取对应的评论信息
		$commentinfo=\PhalApi\DI()->notorm->dynamics_comments
			->where("id='{$commentid}'")
			->fetchOne();

		return $commentinfo;

	}
    /* 评论是否点赞 */
	public function isCommentlike($uid,$commentid) {
        
		$like=\PhalApi\DI()->notorm->dynamics_comments_like
			->select("id")
			->where("uid='{$uid}' and commentid='{$commentid}'")
			->fetchOne();
        if($like){
            return '1';
        }
        
		return '0';
	}
	/* 取消评论点赞- */
	public function reduceCommentLike($uid,$commentid) {
		\PhalApi\DI()->notorm->dynamics_comments_like
						->where("uid='{$uid}' and commentid='{$commentid}'")
						->delete();
			
		$rs=\PhalApi\DI()->notorm->dynamics_comments
			->where("id = '{$commentid}' and likes>0")
			->update( array('likes' => new \NotORM_Literal("likes - 1") ) );
		 
		return $rs;
	}
	/* 评论点赞- */
	public function addCommentLike($uid,$commentid,$touid,$dynamicid) {
		\PhalApi\DI()->notorm->dynamics_comments_like
						->insert(array("uid"=>$uid,"commentid"=>$commentid,"addtime"=>time(),"touid"=>$touid,"dynamicid"=>$dynamicid));
			
		$rs=\PhalApi\DI()->notorm->dynamics_comments
			->where("id = '{$commentid}'")
			->update( array('likes' => new \NotORM_Literal("likes + 1") ) );
		return $rs;
	}

    /* 删除 */
	public function delDynamic($dynamicid) {
		//删除动态
		$rs=\PhalApi\DI()->notorm->dynamics
				->where("id = '{$dynamicid}'")
				->delete();
        //删除动态点赞
        \PhalApi\DI()->notorm->dynamics_like
				->where("dynamicid = '{$dynamicid}'")
				->delete();
		//删除动态评论点赞
		\PhalApi\DI()->notorm->dynamics_comments_like
				->where("dynamicid = '{$dynamicid}'")
				->delete();
		//删除动态评论
		\PhalApi\DI()->notorm->dynamics_comments
				->where("dynamicid = '{$dynamicid}'")
				->delete();		
         //删除动态举报信息
        \PhalApi\DI()->notorm->dynamic_report
				->where("dynamicid = '{$dynamicid}'")
				->delete();
        
		return $rs;
	}

    /* 举报类型 */
	public function getReport() {

		$list=\PhalApi\DI()->notorm->dynamic_report_classify
                ->select('name,name_en')
				->order('list_order asc')
				->fetchAll();
        
		return $list;
	}
    		
	/* 举报 */
	public function setReport($data) {
		$rs=\PhalApi\DI()->notorm->dynamic_report->insert($data);
		return $rs;
	}
    
	//获取关注的用户ID集合
	public function getAttentions($uid){
		$attention=\PhalApi\DI()->notorm->user_attention
				->select("touid")
				->where("uid='{$uid}'")
				->fetchAll();
		return $attention;
	}
	
	 /* 评论列表 */
	public function getComments($where,$p,$order='addtime desc') {
		
        
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;

		$list=\PhalApi\DI()->notorm->dynamics_comments
					->select("*")
					->where($where)
					->order($order)
					->limit($start,$pnum)
					->fetchAll();
		return $list;
	}

	 /* 评论内容 */
	public function getCommentinfo($commentid) {
        
       $info=\PhalApi\DI()->notorm->dynamics_comments
                        ->select("content")
                        ->where("id='{$commentid}'")
                        ->fetchOne();
		return $info;
	}
	 /* 评论信息 */
	public function getReplayCommentinfo($commentid) {
      
       $info=\PhalApi\DI()->notorm->dynamics_comments
                        ->select("*")
                        ->where("commentid='{$commentid}'")
						->order("addtime desc")
                        ->fetchOne();
		return $info;
	}
	 /* 回复内容 */
	public function getReplayinfo($commentid) {
        
       $info=\PhalApi\DI()->notorm->dynamics_comments
					->select("*")
					->where("commentid='{$commentid}'")
					->order("addtime desc")
					//->limit(0,1)
					->fetchAll();
		return $info;
	}
	 /* 回复的评论数量 */
	public function getCommentcount($commentid) {
        
       $info=\PhalApi\DI()->notorm->dynamics_comments
					->where("commentid='{$commentid}'")
					->count();
		return $info;
	}
	/* 动态的评论数量 */
	public function getDynamicount($dynamicid) {
        
       $info=\PhalApi\DI()->notorm->dynamics_comments
					->where("dynamicid='{$dynamicid}'")
					->count();
		return $info;
	}
	 /* 评论回复列表 */
	public function getReplayComments($where,$p,$order='addtime desc') {
		//第一页获取2条数据，从第二页开始每页获取10条数据
        if($p<1){
            $p=1;
        }
		$pnum=10;
		$start=($p-1)*$pnum;

		$list=\PhalApi\DI()->notorm->dynamics_comments
				->select("*")
				->where($where)
				->order($order)
				->limit($start,$pnum)
				->fetchAll();
		return $list;
	}
  
}
